package de.uni_mannheim.informatik.ai.dame_ai.main.ui;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

import de.uni_mannheim.informatik.ai.dame_ai.main.application.GameSettings;
import de.uni_mannheim.informatik.ai.dame_ai.main.ui.MainMenuDictionary.Phrases;

public class GameModusChooserPanel extends ObservablePanel{
	String selectedModus;
	GameSettings game;
	ArrayList<JButton> buttonList;
	public GameModusChooserPanel(int width, int height, File imagePath, GameSettings game){
		super(width, height,imagePath);
		buttonList = new ArrayList<JButton>();
		this.game = game;
		mainDict = MainMenuDictionary.getDictionary(game.getLanguage());
		this.langButton.setText(mainDict.translate(Phrases.main_language));
		//setSize(width, height);
		//this.setBackground(Color.BLUE);
		//setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
	           public void run() {
	        	   insertOptions();
	           }
			}
		);
		
		setPaneLayout(null);
	}
	String[] options;
	public void insertOptions(){
		for(JButton button: buttonList){
			pane.remove(button);
		}
		int x = getWidth()/5*2;
		options = new String[]{mainDict.translate(Phrases.main_new), mainDict.translate(Phrases.main_load), 
				mainDict.translate(Phrases.main_replay), mainDict.translate(Phrases.main_return)};
		if(options != null){
			for(int i = 0; i < options.length; i++){
				System.out.println(options[i]);
				JButton button = addAButton(options[i]);
				button.setBounds(x, getHeight()/18+i*getHeight()/9, getWidth()/5, getHeight()/18);
				if(options[i].equals(mainDict.translate(Phrases.main_return)))
					button.setIcon(new ImageIcon(getScaledImage(returnImg, getHeight()/18, getHeight()/18)));
				buttonList.add(button);
				pane.add(button, new Integer(1));
			}
		}
	}
	public void calculateGeneralBounds(){
		super.calculateGeneralBounds();
		insertOptions();
	}
	public void setFrameReference(JFrame frame){
		this.frameReference = frame;
	}
	@Override
	public void paint(Graphics g){
		//insertOptions();
		super.paint(g);
	}
	private JButton addAButton(String text) {
        JButton button = new JButton(text);
        System.out.println("Added Option Buttons");
        button.addActionListener(new ButtonListener());
        button.setBorder(compound);
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        return button;
    }
	class ButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent button) {
			System.out.println("Button Pressed");
			String selectedOption = button.getActionCommand();
			int choice=-1;
			for (int i = 0; i< options.length;i++){
				if(options[i].equals(selectedOption)){
					choice = i;
				}
			}
			switch(choice){
			case 0:{
				game.setLoadGame(false);
				game.setReplay(false);
				break;
				}
			case 1:{
				game.setLoadGame(true);
				game.setReplay(false);
				break;
				}
			case 2:{
				game.setReplay(true);
				game.setLoadGame(false);
				break;
				}
			case 3:{
				game.setLoadGame(false);
				game.setReplay(false);
				((MainMenuFrame)frameReference).doOnReturnButtonClicked("GameModusChooserPanel");
				break;
				}
			}
			observableObject.setResult(game);
			observableObject.notifyObservers();
		}
	}
}
